package com.gnomon.epsos.service;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class Utils {

    public static String getDocumentAsXml(org.w3c.dom.Document doc, boolean header)
	  {
	  String resp="";       
	  try
	  {
			DOMSource domSource = new DOMSource(doc);
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer transformer = tf.newTransformer();
			String omit="yes";
			if (header) omit="no";else omit="yes";
			transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,omit);
			transformer.setOutputProperty(OutputKeys.METHOD, "xml");
			//transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
			// we want to pretty format the XML output
			// note : this is broken in jdk1.5 beta!
			transformer.setOutputProperty
			   ("{http://xml.apache.org/xslt}indent-amount", "4");
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			//
			java.io.StringWriter sw = new java.io.StringWriter();
			StreamResult sr = new StreamResult(sw);
			transformer.transform(domSource, sr);
			resp = sw.toString();
	  }
	  catch (Exception e)
	  {
		  e.printStackTrace();  
	  }
	  return resp;
	}
	
}
